<template>
  <div class="home-container">
    <van-nav-bar class="app-nav-bar">
      <template #title>
        <van-button
          class="search-btn"
          icon="search"
          type="info"
          round
          to="/search">搜索</van-button>
      </template>
    </van-nav-bar>
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab v-for="(channel,index) in channels" :key="index" :title="channel.name" :name="channel.id">
        <article-list :channel="channel"></article-list>
      </van-tab>
      <div slot="nav-right" class="wap-nav-placeholder"></div>
      <div slot="nav-right" class="wap-nav-wrap" @click="isChannelEditShow = true">
        <van-icon name="wap-nav"></van-icon>
      </div>
    </van-tabs>
    <van-popup
      :style="{ height: '100%' }"
      v-model="isChannelEditShow"
      closeable position="bottom"
      close-icon-position="top-left"
      get-container="body">
        <channel-edit :my-channels="channels" :active="active" @update-active="switchTab"></channel-edit>
      </van-popup>
  </div>
</template>

<script>
import { mapState } from 'Vuex'
import { getUserChannels } from '@/api/user.js'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'HomeIndex',
  props: {},
  components: {
    ChannelEdit,
    ArticleList
  },
  data () {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false
    }
  },
  computed: {
    ...mapState({
      user: state => state.user.user
    })
  },
  watch: {},
  methods: {
    async loadChannels () {
      if (this.user) {
        const { data } = await getUserChannels()
        this.channels = data.data.channels
      } else {
        const localChannels = getItem('TOUTIAO_Channels')
        if (localChannels) {
          this.channels = localChannels
        } else {
          const { data } = await getUserChannels()
          this.channels = data.data.channels
        }
      }
    },
    switchTab (i, isChannelshow = true) {
      this.active = i
      this.isChannelEditShow = isChannelshow
    }
  },
  created () {
    this.loadChannels()
  },
  mounted () {
  },
  destroyed () {
  }
}
</script>

<style scoped lang='less'>
.home-container {
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 277px;
    height: 32px;
    background: #5babfb;
    border: none;
    .van-icon {
      font-size: 16px;
    }
    .van-button__text {
      font-size: 14px;
    }
  }
  .channel-tabs {
    /deep/ .van-tab {
      border-right: 1px solid #edeff3;
      border-bottom: 1px solid #edeff3;
    }
    /deep/ .van-tabs__line {
      width: 15px !important;
      height: 3px;
      background: #3296fa;
      bottom: 20px;
    }
  }
  .wap-nav-placeholder {
    width: 33;
    flex-shrink: 0;
  }
  .wap-nav-wrap {
    position: fixed;
    right: 0;
    width: 33px;
    height: 43px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #fff;
    opacity: .9;
    .van-icon {
      font-size: 24px;
    }
    &:before {
      content: '';
      width: 1px;
      height: 43px;
      background: url('./img/bgc.png') no-repeat;
      background-size: contain;
      position: absolute;
      left: 0;
      top: 0;
    }
  }
}
</style>
